package com.ryan.mdp.client.performance;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;

import com.ryan.mdp.api.normal.DemoMdpService;
import com.ryan.mdp.callback.MdpCallback;
import com.ryan.mdp.dto.DemoDto;

@ContextConfiguration(locations = { "classpath:/context/context-mdp-client.xml" })
public class PerformanceDemoClient extends AbstractJUnit4SpringContextTests {

    private boolean asyncSuccess = false;
    private final int count = 1000;

    @Autowired
    private DemoMdpService demo;

    @org.junit.Test
    public void testSync() {
        for (int i = 0; i < count; i++) {
            demo.echo(1);
            System.out.println(i);
        }
    }

    @org.junit.Test
    public void testAsync() {
        for (int i = 0; i < count; i++) {
            demo.echo("liaosir");
        }
    }

    @org.junit.Test
    public void testCallback() {
        for (int i = 0; i < count; i++) {
            setAsyncSuccess(false);
            demo.echo("liaosir", new MdpCallback() {
                public void invoke(Object object) {
                    System.out.println(object);
                    setAsyncSuccess(true);
                }
            });
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @org.junit.Test(expected = Exception.class)
    public void testException() throws Exception {
        for (int i = 0; i < count; i++) {
            demo.echoException("liaosir");
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @SuppressWarnings("unused")
	@org.junit.Test
    public void testBinary() {
        for (int i = 0; i < count; i++) {
            byte[] result = demo.echoBinary(new byte[] { 1, 2, 3 });
        }
    }

    @org.junit.Test
    public void testDto() {
        for (int i = 0; i < count; i++) {
            DemoDto dto = new DemoDto();
            dto.setProp1("value");
            dto = demo.echoDto(1, new Date(), dto);
            System.out.println(dto.getProp1());
        }
    }

    public boolean isAsyncSuccess() {
        return asyncSuccess;
    }

    public void setAsyncSuccess(boolean asyncSuccess) {
        this.asyncSuccess = asyncSuccess;
    }
}
